package controller;

import cn.edu.zut.model.Book;
import cn.edu.zut.model.BorrowBook;
import cn.edu.zut.model.Reader;
import cn.edu.zut.model.User;
import cn.edu.zut.service.BorrowBookService;
import cn.edu.zut.service.ReaderService;
import cn.edu.zut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 袁飞
 * @date 2019/6/13/01313:54
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    ReaderService readerService;
    @Autowired
    BorrowBookService borrowBookService;

    @Autowired
    Reader reader;
    //检查登录用户
    @RequestMapping("checkLogin")
    public ModelAndView checkLogin(User user, Model model,HttpSession session){
       List<User> users= userService.selectAllUser();
       int flag=0;
        if(user.getName().equals("")||user.getPasswd().equals("")){
            return new ModelAndView("login","message","用户名或者密码不能为空");
        }
        for (User u:users){
            if((u.getName().equals(user.getName()))&&(u.getPasswd().equals(user.getPasswd()))&&(u.getRole()==1)){
                flag=1;
                return new ModelAndView("admin/admin");
            }
            if((u.getName().equals(user.getName()))&&(u.getPasswd().equals(user.getPasswd()))&&(u.getRole()==0)){
                flag=1;
                session.setAttribute("name",user.getName());
                return new ModelAndView("user/user");
            }
        }
        System.out.println(flag);
        if(flag==0){
            return new ModelAndView("login","message","用户名或密码错误！");
        }
        return new ModelAndView("admin/admin");
    }
    //注册用户
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ModelAndView register(User user,HttpSession session){

        List<User> users=userService.selectAllUser();
        if(user.getName().equals("")||user.getPasswd().equals("")){   //用户名或者密码不能为空
            return new ModelAndView("register","message","用户名或者密码不能为空");

        }
        for(User user1:users){
            if(user1.getName().equals(user.getName())){//保证用户名唯一
                return new ModelAndView("register","message","用户名重复，请更换用户名");
            }
        }
        userService.insertUser(user);
        session.setAttribute("readerName",user.getName());//用户名和读者名一致 将用户名传至后台显示欢迎信息
        return new ModelAndView("/readerRegister");
    }
    //查询所有用户
    @RequestMapping("userManager")
    public ModelAndView selectAllUser(HttpSession session) {
        List<User> users = userService.selectAllUser();
        ModelAndView modelAndView=new ModelAndView();
        session.setAttribute("users",users);
        modelAndView.setViewName("admin/userManage");
        return modelAndView;
    }
    //删除用户
    @RequestMapping(value = "userDelete",method = RequestMethod.GET)
    public ModelAndView deleteUser(String name,HttpSession session){
        System.out.println("删除"+name);
        userService.deleteUserByName(name);
        List<User> users= userService.selectAllUser();
        session.setAttribute("users",users);
        return new ModelAndView("admin/userManage","message","删除用户"+name+"成功！");
    }
    //修改用户
    @RequestMapping(value = "userUpdate",method = RequestMethod.GET)
    public ModelAndView updateUser(User user,HttpSession session){
        System.out.println("修改"+user.getName());
        userService.updateUserByName(user);
        List<User> users= userService.selectAllUser();
        session.setAttribute("users",users);
        return new ModelAndView("admin/userManage","message","修改用户"+user.getName()+"成功！");
    }
    //查询所有读者
    @RequestMapping("readerManager")
    public ModelAndView selectAllReader(HttpSession session) {
        List<Reader> readers=readerService.select_All_Reader();
        ModelAndView modelAndView=new ModelAndView();
        session.setAttribute("readers",readers);
        modelAndView.setViewName("admin/reader/readerManage");
        return modelAndView;
    }
    //修改读者
    @RequestMapping(value = "readerUpdate",method = RequestMethod.GET)
    public ModelAndView updateReader(Reader reader,HttpSession session){
        readerService.updateReader(reader);
        List<Reader> readers=readerService.select_All_Reader();
        session.setAttribute("readers",readers);
        return new ModelAndView("admin/reader/readerManage","message","修改读者"+reader.getName()+"成功！");
    }
    //删除读者
    @RequestMapping(value = "readerDelete",method = RequestMethod.GET)
    public ModelAndView deleteReader(String id,HttpSession session){
        System.out.println(id);
        reader=readerService.selectByReaderId(id);
        readerService.deleteReader(reader);
        List<Reader> readers=readerService.select_All_Reader();
        session.setAttribute("readers",readers);
        return new ModelAndView("admin/reader/readerManage","message","删除读者"+reader.getName()+"成功！");
    }
    //读者个人主页处理
    @RequestMapping(value = "showOneUser/{name}",method = RequestMethod.GET)//不同的用户有不同的个人主页url
    public ModelAndView oneUser(@PathVariable("name") String name,HttpSession session){

        Reader reader=readerService.selectByReaderName(name);
        session.setAttribute("reader",reader);
        List<Reader> readers=readerService.select_All_Reader();
        boolean flag=false;//设置该值判断该用户读者信息是否已经注册
        for(Reader reader1:readers){
            if(reader1.getName().equals(name)) {
                flag = true;
            }
        }
        if(!flag){return new ModelAndView("user/reader/error");}//用户未注册读者信息将不能查看个人主页信息
        List<Book> books=borrowBookService.selectBooksByReaderId(reader.getReaderId());//获取该用户的借书信息
        session.setAttribute("books",books);
        List<BorrowBook> borrowBooks=borrowBookService.selectBorrowBookInfo(reader.getReaderId());//获取该用户所借的书本的详细信息
        session.setAttribute("borrowBooks",borrowBooks);
        return new ModelAndView("user/reader/main");
    }
    //退出用户
    @RequestMapping(value = "logOut",method = RequestMethod.GET)
    public ModelAndView logOut(User user){
       // userService.insertUser(user);
        return new ModelAndView("login","message","退出成功");
    }

}
